package com.example.task;

import com.example.entity.IpAddressRecorder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

@Component
@Slf4j
public class MyTask {

    @Autowired
    private IpAddressRecorder ipAddressRecorder;

    @Scheduled(cron = "0/30 * * * * ?") // 每30秒触发一次
    public void executeTask() {
        Set<String> ipAddresses = ipAddressRecorder.getAllIpAddresses();
        log.info("定时任务开始执行，当前记录的 IP 地址数量：{}", ipAddresses.size());
        for (String ipAddress : ipAddresses) {
            log.info("记录的 IP 地址：{}", ipAddress);
        }
        ipAddressRecorder.clear(); // 清空记录，避免重复记录
    }
}
